module EasyForm
  module Tags
    module CollectionExtensions
      private

      def render_collection
        item_wrapper_tag = @options.fetch(:item_wrapper_tag, :span)
        item_wrapper_class = @options[:item_wrapper_class]

        @collection.map do |item|
          value = value_for_collection(item, @value_method)
          text  = value_for_collection(item, @text_method)
          default_html_options = default_html_options_for_collection(item, value)
          additional_html_options = option_html_attributes(item)

          rendered_item = yield item, value, text, default_html_options.merge(additional_html_options)

          #嵌套的在外面包一个label标签
          if @options.fetch(:boolean_style, EasyForm.boolean_style) == :nested
            label_options = default_html_options.slice(:index, :namespace)
            label_options['class'] = @options[:item_label_class]
            rendered_item = @template_object.label(@object_name, sanitize_attribute_name(value), rendered_item, label_options)
          end

          return rendered_item if item_wrapper_tag.blank?
          @template_object.content_tag item_wrapper_tag, rendered_item, class: item_wrapper_class
        end.join.html_safe
      end

      def wrap_rendered_collection(collection)
        wrapper_tag = @options[:collection_wrapper_tag]

        if wrapper_tag
          wrapper_class = @options[:collection_wrapper_class]
          @template_object.content_tag(wrapper_tag, collection, class: wrapper_class)
        else
          collection
        end
      end
    end

    class CollectionRadioButtons < ActionView::Helpers::Tags::CollectionRadioButtons
      include CollectionExtensions

      def render(&block)
        wrap_rendered_collection(super(&block))
      end

      private

      def render_component(builder)
        # if @options.fetch(:boolean_style, EasyForm.boolean_style) == :nested
        #   text = builder.instance_variable_get(:@text)
        #   return builder.radio_button + text
        # end
        label_class = "#{@options[:item_label_class]} collection_redio_buttons".strip
        builder.radio_button + builder.label(class: label_class)
      end
    end

    class CollectionCheckBoxes < ActionView::Helpers::Tags::CollectionCheckBoxes
      include CollectionExtensions

      def render(&block)
        wrap_rendered_collection(super(&block))
      end

      private

      def render_component(builder)
        label_class = "#{@options[:item_label_class]} collection_check_boxes".strip
        builder.check_box + builder.label(class: label_class)
      end

    end
  end
end